<?php

class componentComponents extends sfComponents
{
    public function executeCountrySelectOption()
    {
        $this->countrySelected = $this->countrySelected;
        $this->countryName = $this->countryName;
        $this->countryId = $this->countryId;
    }
    public function executeHeaderInformation()
    {
        $c = new Criteria();
        $c->add(MlmDistributorPeer::STATUS_CODE, Globals::STATUS_ACTIVE);
        $c->add(MlmDistributorPeer::DISTRIBUTOR_ID, $this->param);
        $componentDistributor = MlmDistributorPeer::doSelectOne($c);

        $ecash = 0;
        $epoint = 0;
        $goldDividend = 0;
        $maintenance = 0;
        $monthlySales = 0;
        $unrealizedProfitGold = 0;
        $cps = 0;
        $lastLogin = "";
        if ($componentDistributor) {
            $existUser = AppUserPeer::retrieveByPK($componentDistributor->getUserId());

            if ($existUser) {
                //$timeStamp = strtotime($existUser->getLastLoginDatetime());
                //$lastLogin = date(Globals::FULL_DATETIME_FORMAT, $timeStamp);
                $lastLogin = $existUser->getLastLoginDatetime();
            }

            $this->componentDistributor = $componentDistributor;

            /* *********************************************
             *   ECASH
             * **********************************************/
            $ecash = $this->getAccountBalance($componentDistributor->getDistributorId(), Globals::ACCOUNT_TYPE_ECASH);

            /* *********************************************
             *   EPOINT
             * **********************************************/
            $epoint = $this->getAccountBalance($componentDistributor->getDistributorId(), Globals::ACCOUNT_TYPE_EPOINT);
            /* *********************************************
             *   CPS
             * **********************************************/
            $c = new Criteria();
            $c->add(MlmAccountPeer::ACCOUNT_TYPE, Globals::ACCOUNT_TYPE_CPS);
            $c->add(MlmAccountPeer::DIST_ID, $componentDistributor->getDistributorId());
            $account = MlmAccountPeer::doSelectOne($c);

            if ($account) {
                $cps = $account->getBalance();
            } else {
                $account = new MlmAccount();
                $account->setDistId($componentDistributor->getDistributorId());
                $account->setAccountType(Globals::ACCOUNT_TYPE_CPS);
                $account->setCreatedBy($this->getUser()->getAttribute(Globals::SESSION_USERID, Globals::SYSTEM_USER_ID));
                $account->setUpdatedBy($this->getUser()->getAttribute(Globals::SESSION_USERID, Globals::SYSTEM_USER_ID));
                $account->setBalance(0);
                $account->save();
            }

            /* *********************************************
             *   GOLD DIVIDEND
             * **********************************************/
            $c = new Criteria();
            $c->add(MlmAccountPeer::ACCOUNT_TYPE, Globals::ACCOUNT_TYPE_GOLD);
            $c->add(MlmAccountPeer::DIST_ID, $componentDistributor->getDistributorId());
            $account = MlmAccountPeer::doSelectOne($c);

            if ($account) {
                $goldDividend = $account->getBalance();
            } else {
                $account = new MlmAccount();
                $account->setDistId($componentDistributor->getDistributorId());
                $account->setAccountType(Globals::ACCOUNT_TYPE_GOLD);
                $account->setCreatedBy($this->getUser()->getAttribute(Globals::SESSION_USERID, Globals::SYSTEM_USER_ID));
                $account->setUpdatedBy($this->getUser()->getAttribute(Globals::SESSION_USERID, Globals::SYSTEM_USER_ID));
                $account->setBalance(0);
                $account->save();
            }

            /* *********************************************
             *   MAINTENANCE
             * **********************************************/
            $query = "SELECT SUM(credit-debit) AS SUB_TOTAL FROM mlm_account_ledger WHERE dist_id = " . $componentDistributor->getDistributorId() . " AND account_type = '" . Globals::ACCOUNT_LEDGER_ACTION_MAINTENANCE . "'";

            $connection = Propel::getConnection();
            $statement = $connection->prepareStatement($query);
            $resultset = $statement->executeQuery();
            if ($resultset->next()) {
                $arr = $resultset->getRow();
                if ($arr["SUB_TOTAL"] != null) {
                    $maintenance = $arr["SUB_TOTAL"];
                } else {
                    $tbl_account_ledger = new MlmAccountLedger();
                    $tbl_account_ledger->setDistId($componentDistributor->getDistributorId());
                    $tbl_account_ledger->setAccountType(Globals::ACCOUNT_LEDGER_ACTION_MAINTENANCE);
                    $tbl_account_ledger->setTransactionType(Globals::ACCOUNT_LEDGER_ACTION_REGISTER);
                    $tbl_account_ledger->setCredit(0);
                    $tbl_account_ledger->setDebit(0);
                    $tbl_account_ledger->setBalance(0);
                    $tbl_account_ledger->setRemark("");
                    $tbl_account_ledger->setCreatedBy($this->getUser()->getAttribute(Globals::SESSION_USERID, Globals::SYSTEM_USER_ID));
                    $tbl_account_ledger->setUpdatedBy($this->getUser()->getAttribute(Globals::SESSION_USERID, Globals::SYSTEM_USER_ID));
                    $tbl_account_ledger->save();

                    $maintenance = 0;
                }
            }

            /* *********************************************
             *   MONTHLY SALES
             * **********************************************/
            $dateUtil = new DateUtil();
            $d = $dateUtil->getMonth();
            $firstOfMonth = date('Y-m-j', $d["first_of_month"])." 00:00:00";
            $lastOfMonth = date('Y-m-j', $d["last_of_month"])." 23:59:59";

            $query = "SELECT SUM(credit-debit) AS SUB_TOTAL FROM mlm_dist_pairing_ledger WHERE dist_id = " . $componentDistributor->getDistributorId()
                     . " AND transaction_type = '" . Globals::PAIRING_LEDGER_REGISTER ."'"
                     . " AND created_on >= '". $firstOfMonth . "' AND created_on <= '". $lastOfMonth ."'";

            $connection = Propel::getConnection();
            $statement = $connection->prepareStatement($query);
            $resultset = $statement->executeQuery();
            if ($resultset->next()) {
                $arr = $resultset->getRow();
                if ($arr["SUB_TOTAL"] != null) {
                    $monthlySales = $arr["SUB_TOTAL"];
                } else {
                    $monthlySales = 0;
                }
            }
            /* *********************************************
             *   MONTHLY SALES
             * **********************************************/
            $goldTradingService = new GoldTradingService();
            $goldSell = $goldTradingService->fetchGoldSell();
            $goldBuy = $goldTradingService->fetchGoldBuy($goldSell);

            $query = "SELECT buy_gram * (".$goldBuy." - buy_price) AS unrealizedProfitLoss FROM mlm_egold_account WHERE dist_id = " . $componentDistributor->getDistributorId()
                     . " AND status_code = '" . Globals::EGOLD_ACCOUNT_STATUS_ACTIVE ."'";

            $connection = Propel::getConnection();
            $statement = $connection->prepareStatement($query);
            $resultset = $statement->executeQuery();
            if ($resultset->next()) {
                $arr = $resultset->getRow();
                if ($arr["unrealizedProfitLoss"] != null) {
                    $unrealizedProfitGold = $arr["unrealizedProfitLoss"];
                } else {
                    $unrealizedProfitGold = 0;
                }
            }
        } else {
            $componentDistributor = new MlmDistributor();
        }
        $this->ecash = $ecash;
        $this->epoint = $epoint;
        $this->goldDividend = $goldDividend;
        $this->maintenance = $maintenance;
        $this->monthlySales = $monthlySales;
        $this->lastLogin = $lastLogin;
        $this->componentDistributor = $componentDistributor;
        $this->cps = $cps;
        $this->unrealizedProfitGold = $unrealizedProfitGold;
    }

    function getAccountBalance($distributorId, $accountType)
    {
        $query = "SELECT SUM(credit-debit) AS SUB_TOTAL FROM mlm_account_ledger WHERE dist_id = " . $distributorId . " AND account_type = '" . $accountType . "'";

        $connection = Propel::getConnection();
        $statement = $connection->prepareStatement($query);
        $resultset = $statement->executeQuery();

        if ($resultset->next()) {
            $arr = $resultset->getRow();
            if ($arr["SUB_TOTAL"] != null) {
                return $arr["SUB_TOTAL"];
            } else {
                return 0;
            }
        }
        return 0;
    }
}